package org.crag.cafca.gui.servlet;

import java.io.IOException;
import java.util.SortedSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.crag.cafca.logic.time.Event;
import org.crag.cafca.logic.time.FamilyEvent;
import org.crag.cafca.logic.time.SourceEvent;
import org.crag.cafca.logic.time.SourceManagementEvent;
import org.crag.cafca.logic.time.Tag;
import org.crag.cafca.logic.time.TimeLine;
import org.crag.cafca.util.Utilities;
import org.json.JSONArray;
import org.json.JSONObject;

@WebServlet("/TimelineServlet")
public class TimelineServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

    public TimelineServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		HttpSession session = request.getSession(); 
		TimeLine timeline = (TimeLine) session.getAttribute("timeline"); 
		JSONObject json = createJSonForTimeLineFromMemory(timeline); 
		response.getWriter().print(json.toString());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}
	
	private JSONObject createJSonForTimeLineFromMemory(TimeLine timeline)
	{ 
		JSONObject toSendBack = new JSONObject(); 
		JSONArray dataArray = new JSONArray(); 
		SortedSet<Event> events = timeline.getEvents(); 
		
		for(Event event : events)
		{
			JSONObject row = new JSONObject();
			row.put("start", Utilities.toJSONDateTime(event.getStartDateTime())); 
			
			if (event.getEndDateTime()!=null)
				row.put("end", Utilities.toJSONDateTime(event.getEndDateTime()));
			else
				row.put("end", Utilities.toJSONDateTime(event.getStartDateTime()));				
			
			StringBuffer content = new StringBuffer();
			
			if (event instanceof SourceEvent)
			{
				SourceEvent sourceEvent = (SourceEvent) event;
				content.append("SourceEvent<br/>");
				content.append("<b>" +sourceEvent.getSourceId() + "</b><br/>");
			}

			if (event instanceof FamilyEvent)
			{
				FamilyEvent familyEvent=(FamilyEvent)event;
				content.append("FamilyEvent<br/>");
			}

			if (event instanceof SourceManagementEvent)
			{
				SourceManagementEvent sourceManagementEvent=(SourceManagementEvent)event;
				content.append("SourceManagementEvent<br/>");
				content.append("<b>" +sourceManagementEvent.getSourceId() + "</b><br/>");
			}
			
			content.append(event.getValue()+" <br/>");

			for (Tag tag:event.getTags())
			{
				content.append(tag.getName()+"<br/>"); 				
			}

			row.put("content", content.toString()); 

			dataArray.put(row); 
		}
		
		toSendBack.put("data", dataArray); 
		toSendBack.put("success", true); 
		
		return toSendBack;
	}

}
